package cn.iocoder.yudao.module.qrcode.controller.admin.group;

import cn.iocoder.yudao.module.qrcode.service.group.GroupService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.qrcode.controller.admin.group.vo.*;
import cn.iocoder.yudao.module.qrcode.dal.dataobject.group.GroupDO;

@Tag(name = "管理后台 - 二维码分组")
@RestController
@RequestMapping("/qrcode/group")
@Validated
public class GroupController {

    @Resource
    private GroupService groupService;

    @PostMapping("/create")
    @Operation(summary = "创建二维码分组")
    @PreAuthorize("@ss.hasPermission('qrcode:group:create')")
    public CommonResult<Long> createGroup(@Valid @RequestBody GroupSaveReqVO createReqVO) {
        return success(groupService.createGroup(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新二维码分组")
    @PreAuthorize("@ss.hasPermission('qrcode:group:update')")
    public CommonResult<Boolean> updateGroup(@Valid @RequestBody GroupSaveReqVO updateReqVO) {
        groupService.updateGroup(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除二维码分组")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qrcode:group:delete')")
    public CommonResult<Boolean> deleteGroup(@RequestParam("id") Long id) {
        groupService.deleteGroup(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得二维码分组")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('qrcode:group:query')")
    public CommonResult<GroupRespVO> getGroup(@RequestParam("id") Long id) {
        GroupDO group = groupService.getGroup(id);
        return success(BeanUtils.toBean(group, GroupRespVO.class));
    }

    @PostMapping("/list")
    @Operation(summary = "获得二维码分组列表")
    @PreAuthorize("@ss.hasPermission('qrcode:group:query')")
    public CommonResult<List<GroupRespVO>> getGroupList(@Valid @RequestBody GroupListReqVO listReqVO) {
        List<GroupDO> list = groupService.getGroupList(listReqVO);
        return success(BeanUtils.toBean(list, GroupRespVO.class));
    }

    @PostMapping("/tree")
    @Operation(summary = "获得二维码分组树")
    @PreAuthorize("@ss.hasPermission('qrcode:group:query')")
    public CommonResult<List<GroupRespVO>> getGroupTree(@Valid @RequestBody GroupListReqVO listReqVO) {
        List<GroupRespVO> tree = groupService.getGroupTree(listReqVO);
        return success(tree);
    }

    @PostMapping("/export-excel")
    @Operation(summary = "导出二维码分组 Excel")
    @PreAuthorize("@ss.hasPermission('qrcode:group:export')")
    @OperateLog(type = EXPORT)
    public void exportGroupExcel(@Valid @RequestBody GroupListReqVO listReqVO,
              HttpServletResponse response) throws IOException {
        List<GroupDO> list = groupService.getGroupList(listReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "二维码分组.xls", "数据", GroupRespVO.class,
                        BeanUtils.toBean(list, GroupRespVO.class));
    }

}